<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../../static/css/font.css" th:href="@{/css/font.css}">
    <link rel="stylesheet" href="../../static/css/xadmin.css" th:href="@{/css/xadmin.css}">
    <script src="../../static/js/jquery-3.2.1.min.js" type="text/javascript"
            th:src="@{/js/jquery-3.2.1.min.js}"></script>
    <script src="../../static/js/vue.js" type="text/javascript" th:src="@{/js/vue.js}"></script>
    <script src="../../static/lib/layui/layui.js" charset="utf-8" th:src="@{/lib/layui/layui.js}"></script>
    <script type="text/javascript" src="../../static/js/xadmin.js" th:src="@{/js/xadmin.js}"></script>

    <style>
        .layui-table-cell {
            height: 30px;
        }

        .x-nav {
            margin: 20px;
        }

        .layui-table-cell.laytable-cell-1-0-6 {
            margin: 0px 0px;
        }

        body .selectDiv{
            overflow: visible;
        }
        body .x-nav{
            overflow: visible;
            z-index: 999 !important;
        }
        .layui-form-select dl {
            z-index: 9999;
        }
    </style>

</head>
<body>



<div class="x-nav">


    <div class="layui-col-md8">
        <form class="layui-form">
            <div class="layui-form-item">

                <div class="layui-input-inline">
                    <input type="text" id="quContent1" class="layui-input" placeholder="请输入题目">
                </div>

                <div class="layui-input-inline ">
                    <select id="quType1" name="quType">
                        <option value="">请选择题型</option>
                        <option value="单选题">单选题</option>
                        <option value="多选题">多选题</option>
                        <option value="判断题">判断题</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="subId" id="subId1">
                        <option value="">请选择科目</option>
                        <option th:each="subject:${subjects}" th:value="${subject.subId}" th:text="${subject.subName}">

                        </option>
                    </select>

                </div>
                <div class="layui-inline">
<!--                    <a class="layui-btn query" data-type="reload">查询</a>-->
                    <div class="layui-btn" data-type="reload"  lay-filter="rechar_btn" id="rechar_btn" >
                        <i class="layui-icon layui-icon-search" style="line-height:39px"></i>查询
                    </div>
                </div>
            </div>
        </form>
    </div>

    <div class="layui-col-md4">
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3" style="line-height:40px"></i></a>
    </div>
</div>



<script type="text/html" id="barDemo" >
    <button type="button" class="layui-btn " onclick="x_admin_show('添加试题','/examinfo/exquadd',600,500)">
        <i class="layui-icon layui-icon-add-circle" style="line-height:39px"></i>添加试题
    </button>
    <button type="button" class="layui-btn" onclick="x_admin_show('文件上传','/file/fileupload',700,500)">
        <i class="layui-icon layui-icon-file" style="line-height:39px"></i>批量增加
    </button>
    <a class="layui-btn" lay-event="creatExam">
        <i class="layui-icon layui-icon-form" style="line-height:39px"></i>生成试卷
    </a>
    <a class="layui-btn delAll_btn layui-btn-danger layui-btn" lay-event="delAll">
        <i class="layui-icon layui-icon-delete" style="line-height:39px"></i>批量删除
    </a>


</script>



<table class="layui-hide" id="data" lay-filter="data"></table>
</body>

<script th:inline="none">
    function loadData() {


        layui.use('table', function () {
            let table = layui.table;
            table.render({
                elem: '#data',  //绑定table容器
                url: '/qubank/quallqu',  //请求后端的地址
                limit: 12, //默认值10
                method:'post',
                id: 'examQuListTable',
                toolbar: "#barDemo",
                defaultToolbar: ['filter', 'print', 'exports'],
                //显示的内容
                cols: [ // thymeleaf中，[[]]不能连写，必须换行像如下写法，否则会被解析为内敛表达式
                    [
                        //field:就是成员变量的名字 sort:是否显示排序按钮
                        {field: 'check', width: 50,title:'', type:'checkbox'},
                        {field: 'quId', width: 100, title: '题目编号', sort: true},
                        {field: 'quContent', width: 350, height:30, title: '内容', },
                        {field: 'quAoption', width: 100, title: 'A', },
                        {field: 'quBoption', width: 100, title: 'B', },
                        {field: 'quCoption', width: 100, title: 'C', },
                        {field: 'quDoption', width: 100, title: 'D', },
                        {field: 'quAnswer', width: 100, title: '答案',},
                        {field: 'quType', width: 70, title: '类型', sort: true},
                        {field: 'quScore', width: 100, title: '分数', sort: true},
                        {field: 'subName', width: 100, title: '学科', sort: true},
                        {field: 'upDateTime', width: 150, title: '修改时间', sort: true, templet : "<div>{{layui.util.toDateString(d.upDateTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"},
                        {
                            field: '', width: 150, title: '操作', templet: function (data) {
                                return '<button type="button" class="layui-btn layui-btn-primary layui-btn-sm" onclick="updateOne(this)">\n' +
                                    '    <i class="layui-icon">&#xe642;</i>\n' +
                                    '  </button>' +
                                    '&nbsp;&nbsp;&nbsp;&nbsp;' +
                                    '<button type="button" class="layui-btn layui-btn-primary layui-btn-sm" onclick="deleteOne(this)">\n' +
                                    '    <i class="layui-icon">&#xe640;</i>\n' +
                                    '  </button>';
                            }
                        }
                    ]
                ],
                page: true, //是否开启分页
            });

            $('#rechar_btn').on('click', function(){
                var quContent1 = $("#quContent1").val()
                var quType1 = $("#quType1").val()
                var subId1 = $("#subId1").val()
                var questionBank = {}
                    questionBank.quContent = quContent1,
                    questionBank.quType = quType1,
                    questionBank.subId = subId1
                console.log(questionBank);
                //执行重载
                table.reload('examQuListTable',{
                    elem: '#data',
                    // beforeSend:function () {
                    //     this.layerIndex = layer.load(0, { shade: [0.5, '#393D49'] });
                    // },

                    url:'/qubank/byqu',
                    where:questionBank,
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }

                });
            });



            // 监听
            table.on('toolbar(data)', function (obj) {
                console.log(obj.event);
                if (obj.event == 'delAll'){
                    var checkStatus = table.checkStatus(obj.config.id);
                    data = checkStatus.data;
                    quIds = [];
                    if (data.length > 0) {
                        for (var i in data) {
                            quIds.push(data[i].quId);
                        }
                        console.log(quIds);
                        layer.confirm('确定删除选中的题目？', {icon: 3, title: '提示信息'}, function (index) {
                            $.ajax({
                                url:'/qubank/delqu',
                                type:'post',
                                dataType: 'json',
                                // data:{
                                //     "arr":newsId.toString(),
                                // },
                                data: JSON.stringify(quIds),
                                headers:{
                                    'Content-Type':'application/json;charset=UTF-8',
                                },
                                success:function (rs) {
                                    console.log(rs);
                                    if (rs.code == 200){
                                        layer.open({
                                            title: '删除试题信息'
                                            , content: '删除成功!'
                                        });
                                        loadData();
                                    }else {
                                        layer.open({
                                            title: '删除试题信息'
                                            , content: '删除失败!'
                                        });
                                    }
                                },error:function () {
                                    layer.open({
                                        title: '系统消息'
                                        , content: '服务器错误!'
                                    });
                                }
                            });
                        })
                    } else {
                        layer.msg("请选择需要删除的用户");
                    }
                }
                if (obj.event == 'creatExam'){

                    var checkStatus = table.checkStatus(obj.config.id);
                    data = checkStatus.data;
                    quIds = [];
                    if (data.length > 0) {
                        for (var i in data) {
                            quIds.push(data[i].quId);
                        }
                        console.log(quIds);
                        layer.confirm('确定选中题目？', {
                                //formType: 0,
                                // value: '',
                                title: '请输入试卷名',
                                content: '<input type="text" class="layui-input" id="examName"/>',
                                btn: ['确定','取消'], //按钮，
                                btnAlign: 'c'
                            },
                            function (index) {
                                var examPaperName = $("#examName").val()

                                var data = {
                                    'examPaperName' : examPaperName,
                                    'quIds' : quIds
                                };

                                // data.expaName = expaName;
                                // data.quIds = quIds;

                                console.log(data);
                                $.ajax({
                                    type:"post",
                                    dataType:"json",

                                    contentType: 'application/json; charset=UTF-8',
                                    url: "/expa/addex",
                                    data: JSON.stringify(data),
                                    //     {
                                    //     'examPaperName':examPaperName,
                                    //     'quIds':JSON.stringify(quIds)
                                    // },
                                    // headers:{
                                    //     'Content-Type':'application/json;charset=UTF-8',
                                    // },
                                    success:function (rs) {
                                        console.log(rs);
                                        if (rs.code == 200){
                                            // location.href = "/seller/category/list";
                                            layer.open({
                                                title: '添加试卷信息'
                                                , content: '添加成功!'
                                            });
                                            loadData();
                                        }else {
                                            layer.open({
                                                title: '添加试卷信息'
                                                , content: '添加删除!'
                                            });
                                        }
                                    },error:function () {
                                        layer.open({
                                            title: '系统消息'
                                            , content: '服务器错误!'
                                        });
                                    }
                                });
                                layer.close(index);
                            })
                    } else {
                        layer.msg("请选择生成试卷的题目");
                    }
                }





            })
        })
    }
</script>

<script>
    function deleteOne(obj) {
        var text = $(obj).parent().parent().parent().find("td").eq(1).text();

        $.ajax({
            url: '/qubank/delbyqu',
            type: 'post',
            dataType: 'json',
            data: {
                quId: text,
            },

            success: function (data) {
                if (data.code == 200) {
                    layer.open({
                        title: '删除试题信息'
                        , content: '删除成功!'
                    });
                    // 清除当前这个tr
                    $(obj).parents("tr").remove();
                } else {
                    layer.open({
                        title: '删除试题信息'
                        , content: '删除失败!'
                    });
                }
            }, error: function () {
                layer.open({
                    title: '系统消息'
                    , content: '服务器错误!'
                });
            }
        })
    }
</script>



<script>
    function updateOne(obj) {
        var quId = $(obj).parent().parent().parent().find("td").eq(1).text();

        x_admin_show('编辑', '/examinfo/upexqu/' + quId, 600, 400);
    }

    function updateOne2() {
        $.ajax({
            type:"post",
            dataType:"json",

            url: "/expa/addex",
            data: {
                "examPaperName": "121",
                "quIds": [
                    74,
                    75
                ]
            }
        })
    }

</script>


<!--   文档加载事件-->
<script>
    $(function () {
        loadData();
    });
</script>

</html>